package commRedisService

import "gitee.com/fierce_wolf/go-fox-edge-common/commEntity"

func GetUpdateTime(entityType string) int64 {
	return IRedisManager.GetUpdateTime(entityType)
}

func GetEntityById(entityType string, id int64) commEntity.IEntity {
	return IRedisManager.GetEntityById(entityType, id)
}

func ReadEntityByServiceKey(entityType string, serviceKey string) (commEntity.IEntity, error) {
	return IRedisManager.ReadEntityByServiceKey(entityType, serviceKey)
}

func GetEntityByServiceKey(entityType string, serviceKey string) (commEntity.IEntity, error) {
	return IRedisManager.GetEntityByServiceKey(entityType, serviceKey)
}

func GetEntityByServiceKeys(entityType string, serviceKeys []string) (map[string]commEntity.IEntity, error) {
	return IRedisManager.GetEntityByServiceKeys(entityType, serviceKeys)
}

func HasEntityByServiceKey(entityType string, serviceKey string) (bool, error) {
	entity, err := IRedisManager.GetEntityByServiceKey(entityType, serviceKey)
	if err != nil {
		return false, err
	}

	return entity != nil, err
}

func WriteEntity(entity commEntity.IEntity) error {
	return IRedisManager.WriteEntity(entity)
}

func UpdateEntity(entity commEntity.IEntity) error {
	return IRedisManager.UpdateEntity(entity)
}

func DeleteEntity(entityType string, key string) {
	IRedisManager.DeleteEntity(entityType, key)
}

func InsertEntity(entity commEntity.IEntity) {
	IRedisManager.InsertEntity(entity)
}

func GetDeviceEntity(deviceName string) *commEntity.DeviceEntity {
	entity := commEntity.DeviceEntity{}
	entity.DeviceName = deviceName

	value, err := IRedisManager.GetEntityByServiceKey(commEntity.Type.DeviceEntity.Type(), entity.MakeServiceKey())
	if err != nil || value == nil {
		return nil
	}

	out, _ := value.(*commEntity.DeviceEntity)
	return out
}

func GetChannelEntity(channelName string, channelType string) *commEntity.ChannelEntity {
	entity := commEntity.ChannelEntity{}
	entity.ChannelName = channelName
	entity.ChannelType = channelType

	value, err := IRedisManager.GetEntityByServiceKey(commEntity.Type.ChannelEntity.Type(), entity.MakeServiceKey())
	if err != nil || value == nil {
		return nil
	}

	out, _ := value.(*commEntity.ChannelEntity)
	return out
}

func GetConfigEntity(serviceName string, serviceType string, configName string) *commEntity.ConfigEntity {
	entity := commEntity.ConfigEntity{}
	entity.ServiceType = serviceType
	entity.ServiceName = serviceName
	entity.ConfigName = configName

	value, err := IRedisManager.GetEntityByServiceKey(commEntity.Type.ConfigEntity.Type(), entity.MakeServiceKey())
	if err != nil || value == nil {
		return nil
	}

	out, _ := value.(*commEntity.ConfigEntity)
	return out
}

func GetOperateEntity(manufacturer string, deviceType string, operateName string) *commEntity.OperateEntity {
	entity := commEntity.OperateEntity{}
	entity.Manufacturer = manufacturer
	entity.DeviceType = deviceType
	entity.OperateName = operateName

	value, err := IRedisManager.GetEntityByServiceKey(commEntity.Type.OperateEntity.Type(), entity.MakeServiceKey())
	if err != nil || value == nil {
		return nil
	}

	out, _ := value.(*commEntity.OperateEntity)
	return out
}

func ForeachEntityList(entityType string, finder func(entity commEntity.IEntity) bool) {
	IRedisManager.ForeachEntityList(entityType, finder)
}

func FindEntityList(entityType string, finder func(entity commEntity.IEntity) bool) []commEntity.IEntity {
	return IRedisManager.FindEntityList(entityType, finder)
}

func FindEntity(entityType string, finder func(entity commEntity.IEntity) bool) commEntity.IEntity {
	return IRedisManager.FindEntity(entityType, finder)
}

func GetEntityCount(entityType string, finder func(entity commEntity.IEntity) bool) (int, error) {
	return IRedisManager.GetEntityCount(entityType, finder)
}

func GetEntityMap(entityType string) (map[string]commEntity.IEntity, error) {
	return IRedisManager.GetEntityMap(entityType)
}

func GetEntityList(entityType string) ([]commEntity.IEntity, error) {
	mps, err := IRedisManager.GetEntityMap(entityType)
	if err != nil {
		return nil, err
	}

	list := make([]commEntity.IEntity, 0)
	for _, v := range mps {
		list = append(list, v)
	}

	return list, err
}

func BindTypeNotify(entityType string, notify ConsumerTypeNotify) {
	IRedisManager.BindTypeNotify(entityType, notify)
}

func GetAgileService(entityType string) *RedisAgileService {
	return IRedisAgileManager.GetAgileService(entityType)
}
